home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / make-367.lha / make-3.67 / read.c < prev    next >
C/C++ Source or Header  |  1993-05-19  |  47KB  |  1,769 lines

  1. /* Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993
  2.     Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20. #include "commands.h"
  21. #include "dep.h"
  22. #include "file.h"
  23. #include "variable.h"
  24.  
  25. /* This is POSIX.2, but most systems using -DPOSIX probably don't have it.  */
  26. #ifdef    HAVE_GLOB_H
  27. #include <glob.h>
  28. #else
  29. #include "glob/glob.h"
  30. #endif
  31.  
  32. #include <pwd.h>
  33. struct passwd *getpwnam ();
  34.  
  35.  
  36. static void read_makefile ();
  37. static unsigned int readline (), do_define ();
  38. static int conditional_line ();
  39. static void record_files ();
  40. static char *find_semicolon ();
  41.  
  42.  
  43. /* A `struct linebuffer' is a structure which holds a line of text.
  44.    `readline' reads a line from a stream into a linebuffer
  45.    and works regardless of the length of the line.  */
  46.  
  47. struct linebuffer
  48.   {
  49.     /* Note:  This is the number of bytes malloc'ed for `buffer'
  50.        It does not indicate `buffer's real length.
  51.        Instead, a null char indicates end-of-string.  */
  52.     unsigned int size;
  53.     char *buffer;
  54.   };
  55.  
  56. #define initbuffer(lb) (lb)->buffer = (char *) xmalloc ((lb)->size = 200)
  57. #define freebuffer(lb) free ((lb)->buffer)
  58.  
  59.  
  60. /* A `struct conditionals' contains the information describing
  61.    all the active conditionals in a makefile.
  62.  
  63.    The global variable `conditionals' contains the conditionals
  64.    information for the current makefile.  It is initialized from
  65.    the static structure `toplevel_conditionals' and is later changed
  66.    to new structures for included makefiles.  */
  67.  
  68. struct conditionals
  69.   {
  70.     unsigned int if_cmds;    /* Depth of conditional nesting.  */
  71.     unsigned int allocated;    /* Elts allocated in following arrays.  */
  72.     char *ignoring;        /* Are we ignoring or interepreting?  */
  73.     char *seen_else;        /* Have we already seen an `else'?  */
  74.   };
  75.  
  76. static struct conditionals toplevel_conditionals;
  77. static struct conditionals *conditionals = &toplevel_conditionals;
  78.   
  79.  
  80. /* Default directories to search for include files in  */
  81.  
  82. static char *default_include_directories[] =
  83.   {
  84.     INCLUDEDIR,
  85.     "/usr/gnu/include",
  86.     "/usr/local/include",
  87.     "/usr/include",
  88.     0
  89.   };
  90.  
  91. /* List of directories to search for include files in  */
  92.  
  93. static char **include_directories;
  94.  
  95. /* Maximum length of an element of the above.  */
  96.  
  97. static unsigned int max_incl_len;
  98.  
  99. /* The filename and pointer to line number of the
  100.    makefile currently being read in.  */
  101.  
  102. char *reading_filename;
  103. unsigned int *reading_lineno_ptr;
  104.  
  105. /* The chain of makefiles read by read_makefile.  */
  106.  
  107. static struct dep *read_makefiles = 0;
  108.  
  109. /* Read in all the makefiles and return the chain of their names.  */
  110.  
  111. struct dep *
  112. read_all_makefiles (makefiles)
  113.      char **makefiles;
  114. {
  115.   unsigned int num_makefiles = 0;
  116.  
  117.   if (debug_flag)
  118.     puts ("Reading makefiles...");
  119.  
  120.   /* If there's a non-null variable MAKEFILES, its value is a list of
  121.      files to read first thing.  But don't let it prevent reading the
  122.      default makefiles and don't let the default goal come from there.  */
  123.  
  124.   {
  125.     char *value = allocated_variable_expand ("$(MAKEFILES)");
  126.     char *name, *p;
  127.     unsigned int length;
  128.  
  129.     /* Set NAME to the start of next token and LENGTH to its length.
  130.        MAKEFILES is updated for finding remaining tokens.  */
  131.     p = value;
  132.     while ((name = find_next_token (&p, &length)) != 0)
  133.       {
  134.     if (*p != '\0')
  135.       *p++ = '\0';
  136.     read_makefile (name, 1);
  137.       }
  138.  
  139.     free (value);
  140.   }
  141.  
  142.   /* Read makefiles specified with -f switches.  */
  143.  
  144.   if (makefiles != 0)
  145.     while (*makefiles != 0)
  146.       {
  147.     struct dep *tail = read_makefiles;
  148.     register struct dep *d;
  149.  
  150.     read_makefile (*makefiles, 0);
  151.  
  152.     /* Find the right element of read_makefiles.  */
  153.     d = read_makefiles;
  154.     while (d->next != tail)
  155.       d = d->next;
  156.  
  157.     /* Use the storage read_makefile allocates.  */
  158.     free (*makefiles);
  159.     *makefiles = dep_name (d);
  160.     ++num_makefiles;
  161.     ++makefiles;
  162.       }
  163.  
  164.   /* If there were no -f switches, try the default names.  */
  165.  
  166.   if (num_makefiles == 0)
  167.     {
  168.       static char *default_makefiles[] =
  169.     { "GNUmakefile", "makefile", "Makefile", 0 };
  170.       register char **p = default_makefiles;
  171.       while (*p != 0 && !file_exists_p (*p))
  172.     ++p;
  173.  
  174.       if (*p != 0)
  175.     read_makefile (*p, 0);
  176.       else
  177.     {
  178.       /* No default makefile was found.  Add the default makefiles to the
  179.          `read_makefiles' chain so they will be updated if possible.  */
  180.       struct dep *tail = read_makefiles;
  181.       for (p = default_makefiles; *p != 0; ++p)
  182.         {
  183.           struct dep *d = (struct dep *) xmalloc (sizeof (struct dep));
  184.           d->name = 0;
  185.           d->file = enter_file (*p);
  186.           d->file->dontcare = 1;
  187.           /* Setting the `changed' member to 1 will make failure to
  188.          update or find this makefile as if it had come from the
  189.          MAKEFILES variable: we don't care, so we won't die.  */
  190.           d->changed = 1;
  191.           if (tail == 0)
  192.         read_makefiles = d;
  193.           else
  194.         tail->next = d;
  195.           tail = d;
  196.         }
  197.       if (tail != 0)
  198.         tail->next = 0;
  199.     }
  200.     }
  201.  
  202.   return read_makefiles;
  203. }
  204.  
  205. /* Read file FILENAME as a makefile and add its contents to the data base.
  206.  
  207.    TYPE indicates what flavor of makefile this is: 0 => a default or -f
  208.    makefile (the basis for comparison); 1 => from the MAKEFILES variable:
  209.    cannot determine the default goal, is searched for in the search path,
  210.    and it's not an error if it doesn't exist; 2 => an included makefile:
  211.    is searched for in the search path.
  212.  
  213.    FILENAME is added to the `read_makefiles' chain.  */
  214.  
  215. static void
  216. read_makefile (filename, type)
  217.      char *filename;
  218.      int type;
  219. {
  220.   static char *collapsed = 0;
  221.   static unsigned int collapsed_length = 0;
  222.   register FILE *infile;
  223.   struct linebuffer lb;
  224.   unsigned int commands_len = 200;
  225.   char *commands = (char *) xmalloc (200);
  226.   unsigned int commands_idx = 0;
  227.   unsigned int commands_started;
  228.   register char *p;
  229.   char *p2;
  230.   int ignoring = 0, in_ignored_define = 0;
  231.   int no_targets = 0;        /* Set when reading a rule without targets.  */
  232.  
  233.   struct nameseq *filenames = 0;
  234.   struct dep *deps;
  235.   unsigned int lineno = 1;
  236.   unsigned int nlines = 0;
  237.   int two_colon;
  238.   char *pattern = 0, *pattern_percent;
  239.  
  240.   int makefile_errno;
  241.  
  242. #define record_waiting_files()                              \
  243.   do                                          \
  244.     {                                           \
  245.       if (filenames != 0)                              \
  246.     record_files (filenames, pattern, pattern_percent, deps,          \
  247.               commands_started, commands, commands_idx,              \
  248.               two_colon, filename, lineno, type != 1);              \
  249.       filenames = 0;                                  \
  250.       commands_idx = 0;                                  \
  251.       pattern = 0;                                  \
  252.     } while (0)
  253.  
  254. #ifdef    lint    /* Suppress `used before set' messages.  */
  255.   two_colon = 0;
  256. #endif
  257.  
  258.   /* First, get a stream to read.  */
  259.  
  260.   infile = fopen (filename, "r");
  261.   /* Save the error code so we print the right message later.  */
  262.   makefile_errno = errno;
  263.  
  264.   /* If the makefile wasn't found and it's either a makefile from
  265.      the `MAKEFILES' variable (type 1) or an included makefile (type 2),
  266.      search the included makefile search path for this makefile.  */
  267.  
  268.   if (infile == 0 && (type == 1 || type == 2) && *filename != '/')
  269.     {
  270.       register unsigned int i;
  271.       for (i = 0; include_directories[i] != 0; ++i)
  272.     {
  273.       char *name = concat (include_directories[i], "/", filename);
  274.       infile = fopen (name, "r");
  275.       if (infile == 0)
  276.         free (name);
  277.       else
  278.         {
  279.           filename = name;
  280.           break;
  281.         }
  282.     }
  283.     }
  284.  
  285.   /* Add FILENAME to the chain of read makefiles.  */
  286.   deps = (struct dep *) xmalloc (sizeof (struct dep));
  287.   deps->next = read_makefiles;
  288.   read_makefiles = deps;
  289.   deps->name = 0;
  290.   deps->file = lookup_file (filename);
  291.   if (deps->file == 0)
  292.     {
  293.       deps->file = enter_file (savestring (filename, strlen (filename)));
  294.       if (type == 1)
  295.     deps->file->dontcare = 1;
  296.     }
  297.   filename = deps->file->name;
  298.   deps->file->precious = 1;
  299.   deps->changed = type;
  300.   deps = 0;
  301.  
  302.   /* If the makefile can't be found at all,
  303.      either ignore it or give up entirely.  */
  304.  
  305.   if (infile == 0)
  306.     {
  307.       if (type != 1)
  308.     {
  309.       /* If we did some searching, errno has the error
  310.          from the last attempt, rather from FILENAME itself.  */
  311.       errno = makefile_errno;
  312.       perror_with_name ("fopen: ", filename);
  313.     }
  314.       return;
  315.     }
  316.  
  317.   reading_filename = filename;
  318.   reading_lineno_ptr = &lineno;
  319.  
  320.   /* Loop over lines in the file.
  321.      The strategy is to accumulate target names in FILENAMES, dependencies
  322.      in DEPS and commands in COMMANDS.  These are used to define a rule
  323.      when the start of the next rule (or eof) is encountered.  */
  324.  
  325.   initbuffer (&lb);
  326.  
  327.   while (!feof (infile))
  328.     {
  329.       lineno += nlines;
  330.       nlines = readline (&lb, infile, filename, lineno);
  331.  
  332.       if (collapsed_length < lb.size)
  333.     {
  334.       collapsed_length = lb.size;
  335.       if (collapsed != 0)
  336.         free (collapsed);
  337.       collapsed = (char *) xmalloc (collapsed_length);
  338.     }
  339.       strcpy (collapsed, lb.buffer);
  340.       /* Collapse continuation lines.  */
  341.       collapse_continuations (collapsed);
  342.       remove_comments (collapsed);
  343.  
  344.       p = collapsed;
  345.       while (isspace (*p))
  346.     ++p;
  347.       /* We cannot consider a line containing just a tab to be empty
  348.      because it might constitute an empty command for a target.  */
  349.       if (*p == '\0' && lb.buffer[0] != '\t')
  350.     continue;
  351.  
  352.       /* strncmp is first to avoid dereferencing out into space.  */
  353. #define    word1eq(s, l)     (!strncmp (s, p, l) \
  354.              && (p[l] == '\0' || isblank (p[l])))
  355.       if (!in_ignored_define
  356.       && (word1eq ("ifdef", 5) || word1eq ("ifndef", 6)
  357.           || word1eq ("ifeq", 4) || word1eq ("ifneq", 5)
  358.           || word1eq ("else", 4) || word1eq ("endif", 5)))
  359.     {
  360.       int i = conditional_line (p, filename, lineno);
  361.       if (i >= 0)
  362.         ignoring = i;
  363.       else
  364.         makefile_fatal (filename, lineno,
  365.                 "invalid syntax in conditional");
  366.       continue;
  367.     }
  368.       else if (word1eq ("endef", 5))
  369.     {
  370.       if (in_ignored_define)
  371.         in_ignored_define = 0;
  372.       else
  373.         makefile_fatal (filename, lineno, "extraneous `endef'");
  374.       continue;
  375.     }
  376.       else if (word1eq ("define", 6))
  377.     {
  378.       if (ignoring)
  379.         in_ignored_define = 1;
  380.       else
  381.         {
  382.           p2 = next_token (p + 6);
  383.           p = end_of_token (p2);
  384.           lineno = do_define (p2, p - p2, o_file,
  385.                   lineno, infile, filename);
  386.         }
  387.       continue;
  388.     }
  389.       else if (word1eq ("override", 8))
  390.     {
  391.       p2 = next_token (p + 8);
  392.       if (p2 == 0)
  393.         makefile_error (filename, lineno, "empty `override' directive");
  394.       if (!strncmp (p2, "define", 6) && (isblank (p2[6]) || p2[6] == '\0'))
  395.         {
  396.           if (ignoring)
  397.         in_ignored_define = 1;
  398.           else
  399.         {
  400.           unsigned int len;
  401.           p2 = end_of_token (p2);
  402.           p = find_next_token (&p2, &len);
  403.           lineno = do_define (p, len, o_override,
  404.                       lineno, infile, filename);
  405.         }
  406.         }
  407.       else if (!ignoring
  408.            && !try_variable_definition (filename, lineno,
  409.                         p2, o_override))
  410.         makefile_error (filename, lineno, "empty `override' directive");
  411.  
  412.       continue;
  413.     }
  414.  
  415.  
  416.       if (ignoring)
  417.     /* Ignore the line.  We continue here so conditionals
  418.        can appear in the middle of a rule.  */
  419.     continue;
  420.       else if (lb.buffer[0] == '\t')
  421.     {
  422.       /* This line is a shell command.  */
  423.       unsigned int len;
  424.  
  425.       if (no_targets)
  426.         /* Ignore the commands in a rule with no targets.  */
  427.         continue;
  428.  
  429.       /* If there is no preceding rule line, don't treat this line
  430.          as a command, even though it begins with a tab character.
  431.          SunOS 4 make appears to behave this way.  */
  432.  
  433.       if (filenames != 0)
  434.         {
  435.           /* Append this command line to the line being accumulated.  */
  436.           p = lb.buffer;
  437.           if (commands_idx == 0)
  438.         commands_started = lineno;
  439.           len = strlen (p);
  440.           if (len + 1 + commands_idx > commands_len)
  441.         {
  442.           commands_len = (len + 1 + commands_idx) * 2;
  443.           commands = (char *) xrealloc (commands, commands_len);
  444.         }
  445.           bcopy (p, &commands[commands_idx], len);
  446.           commands_idx += len;
  447.           commands[commands_idx++] = '\n';
  448.  
  449.           continue;
  450.         }
  451.     }
  452.  
  453.       if (word1eq ("export", 6))
  454.     {
  455.       struct variable *v;
  456.       p2 = next_token (p + 6);
  457.       if (*p2 == '\0')
  458.         export_all_variables = 1;
  459.       v = try_variable_definition (filename, lineno, p2, o_file);
  460.       if (v != 0)
  461.         v->export = v_export;
  462.       else
  463.         {
  464.           unsigned int len;
  465.           for (p = find_next_token (&p2, &len); p != 0;
  466.            p = find_next_token (&p2, &len))
  467.         {
  468.           v = lookup_variable (p, len);
  469.           if (v == 0)
  470.             v = define_variable (p, len, "", o_file, 0);
  471.           v->export = v_export;
  472.         }
  473.         }
  474.     }
  475.       else if (word1eq ("unexport", 8))
  476.     {
  477.       unsigned int len;
  478.       struct variable *v;
  479.       p2 = next_token (p + 8);
  480.       if (*p2 == '\0')
  481.         export_all_variables = 0;
  482.       for (p = find_next_token (&p2, &len); p != 0;
  483.            p = find_next_token (&p2, &len))
  484.         {
  485.           v = lookup_variable (p, len);
  486.           if (v == 0)
  487.         v = define_variable (p, len, "", o_file, 0);
  488.           v->export = v_noexport;
  489.         }
  490.     }
  491.       else if (word1eq ("include", 7))
  492.     {
  493.       /* We have found an `include' line specifying a nested
  494.          makefile to be read at this point.  */
  495.       struct conditionals *save, new_conditionals;
  496.       struct nameseq *files;
  497.  
  498.       p = allocated_variable_expand (next_token (p + 8));
  499.       if (*p == '\0')
  500.         {
  501.           makefile_error (filename, lineno, "no file name for `include'");
  502.           continue;
  503.         }
  504.  
  505.       /* Parse the list of file names.  */
  506.       p2 = p;
  507.       files = multi_glob (parse_file_seq (&p2, '\0',
  508.                           sizeof (struct nameseq),
  509.                           1),
  510.                   sizeof (struct nameseq));
  511.       free (p);
  512.  
  513.       /* Save the state of conditionals and start
  514.          the included makefile with a clean slate.  */
  515.       save = conditionals;
  516.       bzero ((char *) &new_conditionals, sizeof new_conditionals);
  517.       conditionals = &new_conditionals;
  518.  
  519.       /* Record the rules that are waiting so they will determine
  520.          the default goal before those in the included makefile.  */
  521.       record_waiting_files ();
  522.  
  523.       /* Read each included makefile.  */
  524.       while (files != 0)
  525.         {
  526.           struct nameseq *next = files->next;
  527.           char *name = files->name;
  528.           free (files);
  529.           files = next;
  530.  
  531.           read_makefile (name, 2);
  532.         }
  533.  
  534.       /* Restore state.  */
  535.       conditionals = save;
  536.       reading_filename = filename;
  537.       reading_lineno_ptr = &lineno;
  538.     }
  539.       else if (word1eq ("vpath", 5))
  540.     {
  541.       char *pattern;
  542.       unsigned int len;
  543.       p2 = variable_expand (p + 5);
  544.       p = find_next_token (&p2, &len);
  545.       if (p != 0)
  546.         {
  547.           pattern = savestring (p, len);
  548.           p = find_next_token (&p2, &len);
  549.           /* No searchpath means remove all previous
  550.          selective VPATH's with the same pattern.  */
  551.         }
  552.       else
  553.         /* No pattern means remove all previous selective VPATH's.  */
  554.         pattern = 0;
  555.       construct_vpath_list (pattern, p);
  556.       if (pattern != 0)
  557.         free (pattern);
  558.     }
  559. #undef    word1eq
  560.       else if (try_variable_definition (filename, lineno, p, o_file))
  561.     /* This line has been dealt with.  */
  562.     ;
  563.       else
  564.     {
  565.       /* This line describes some target files.  */
  566.  
  567.       char *cmdleft;
  568.  
  569.       /* Record the previous rule.  */
  570.  
  571.       record_waiting_files ();
  572.  
  573.       /* Look for a semicolon in the unexpanded line.  */
  574.       cmdleft = find_semicolon (lb.buffer);
  575.       if (cmdleft != 0)
  576.         /* Found one.  Cut the line short there before expanding it.  */
  577.         *cmdleft = '\0';
  578.  
  579.       collapse_continuations (lb.buffer);
  580.  
  581.       /* Expand variable and function references before doing anything
  582.          else so that special characters can be inside variables.  */
  583.       p = variable_expand (lb.buffer);
  584.  
  585.       if (cmdleft == 0)
  586.         /* Look for a semicolon in the expanded line.  */
  587.         cmdleft = find_semicolon (p);
  588.  
  589.       if (cmdleft != 0)
  590.         /* Cut the line short at the semicolon.  */
  591.         *cmdleft = '\0';
  592.  
  593.       /* Remove comments from the line.  */
  594.       remove_comments (p);
  595.  
  596.       p2 = next_token (p);
  597.       if (*p2 == '\0')
  598.         {
  599.           if (cmdleft != 0)
  600.         makefile_fatal (filename, lineno,
  601.                 "missing rule before commands");
  602.           else
  603.         /* This line contained a variable reference that
  604.            expanded to nothing but whitespace.  */
  605.         continue;
  606.         }
  607.       else if (*p2 == ':')
  608.         {
  609.           /* We accept and ignore rules without targets for
  610.          compatibility with SunOS 4 make.  */
  611.           no_targets = 1;
  612.           continue;
  613.         }
  614.  
  615.       filenames = multi_glob (parse_file_seq (&p2, ':',
  616.                           sizeof (struct nameseq)),
  617.                   sizeof (struct nameseq),
  618.                   1);
  619.       if (*p2++ == '\0')
  620.         makefile_fatal (filename, lineno, "missing separator");
  621.       /* Is this a one-colon or two-colon entry?  */
  622.       two_colon = *p2 == ':';
  623.       if (two_colon)
  624.         p2++;
  625.  
  626.       /* We have some targets, so don't ignore the following commands.  */
  627.       no_targets = 0;
  628.  
  629.       /* Is this a static pattern rule: `target: %targ: %dep; ...'?  */
  630.       p = index (p2, ':');
  631.       while (p != 0 && p[-1] == '\\')
  632.         {
  633.           register char *q = &p[-1];
  634.           register int backslash = 0;
  635.           while (*q-- == '\\')
  636.         backslash = !backslash;
  637.           if (backslash)
  638.         p = index (p + 1, ':');
  639.           else
  640.         break;
  641.         }
  642.       if (p != 0)
  643.         {
  644.           struct nameseq *target;
  645.           target = parse_file_seq (&p2, ':', sizeof (struct nameseq), 1);
  646.           ++p2;
  647.           if (target == 0)
  648.         makefile_fatal (filename, lineno, "missing target pattern");
  649.           else if (target->next != 0)
  650.         makefile_fatal (filename, lineno, "multiple target patterns");
  651.           pattern = target->name;
  652.           pattern_percent = find_percent (pattern);
  653.           if (pattern_percent == 0)
  654.         makefile_fatal (filename, lineno,
  655.                 "target pattern contains no `%%'");
  656.         }
  657.       else
  658.         pattern = 0;
  659.  
  660.       /* Parse the dependencies.  */
  661.       deps = (struct dep *)
  662.         multi_glob (parse_file_seq (&p2, '\0', sizeof (struct dep)),
  663.             sizeof (struct dep),
  664.             1);
  665.  
  666.       commands_idx = 0;
  667.       if (cmdleft != 0)
  668.         {
  669.           /* Semicolon means rest of line is a command.  */
  670.           unsigned int len = strlen (cmdleft + 1);
  671.  
  672.           commands_started = lineno;
  673.  
  674.           /* Add this command line to the buffer.  */
  675.           if (len + 2 > commands_len)
  676.         {
  677.           commands_len = (len + 2) * 2;
  678.           commands = (char *) xrealloc (commands, commands_len);
  679.         }
  680.           bcopy (cmdleft + 1, commands, len);
  681.           commands_idx += len;
  682.           commands[commands_idx++] = '\n';
  683.         }
  684.  
  685.       continue;
  686.     }
  687.  
  688.       /* We get here except in the case that we just read a rule line.
  689.      Record now the last rule we read, so following spurious
  690.      commands are properly diagnosed.  */
  691.       record_waiting_files ();
  692.       no_targets = 0;
  693.     }
  694.  
  695.   if (conditionals->if_cmds)
  696.     makefile_fatal (filename, lineno, "missing `endif'");
  697.  
  698.   /* At eof, record the last rule.  */
  699.   record_waiting_files ();
  700.  
  701.   freebuffer (&lb);
  702.   free ((char *) commands);
  703.   fclose (infile);
  704.  
  705.   reading_filename = 0;
  706.   reading_lineno_ptr = 0;
  707. }
  708.  
  709. /* Execute a `define' directive.
  710.    The first line has already been read, and NAME is the name of
  711.    the variable to be defined.  The following lines remain to be read.
  712.    LINENO, INFILE and FILENAME refer to the makefile being read.
  713.    The value returned is LINENO, updated for lines read here.  */
  714.  
  715. static unsigned int
  716. do_define (name, namelen, origin, lineno, infile, filename)
  717.      char *name;
  718.      unsigned int namelen;
  719.      enum variable_origin origin;
  720.      unsigned int lineno;
  721.      FILE *infile;
  722.      char *filename;
  723. {
  724.   struct linebuffer lb;
  725.   unsigned int nlines = 0;
  726.   unsigned int length = 100;
  727.   char *definition = (char *) xmalloc (100);
  728.   register unsigned int idx = 0;
  729.   register char *p;
  730.  
  731.   /* Expand the variable name.  */
  732.   char *var = (char *) alloca (namelen + 1);
  733.   bcopy (name, var, namelen);
  734.   var[namelen] = '\0';
  735.   var = variable_expand (var);
  736.  
  737.   initbuffer (&lb);
  738.   while (!feof (infile))
  739.     {
  740.       lineno += nlines;
  741.       nlines = readline (&lb, infile, filename, lineno);
  742.       p = next_token (lb.buffer);
  743.  
  744.       if ((p[5] == '\0' || isblank (p[5])) && !strncmp (p, "endef", 5))
  745.     {
  746.       p += 5;
  747.       collapse_continuations (p);
  748.       remove_comments (p);
  749.       if (*next_token (p) != '\0')
  750.         makefile_error (filename, lineno,
  751.                 "Extraneous text after `endef' directive");
  752.       /* Define the variable.  */
  753.       if (idx == 0)
  754.         definition[0] = '\0';
  755.       else
  756.         definition[idx - 1] = '\0';
  757.       (void) define_variable (var, strlen (var), definition, origin, 1);
  758.       free (definition);
  759.       freebuffer (&lb);
  760.       return lineno;
  761.     }
  762.       else
  763.     {
  764.       unsigned int len = strlen (p);
  765.  
  766.       /* Increase the buffer size if necessary.  */
  767.       if (idx + len + 1 > length)
  768.         {
  769.           length = (idx + len) * 2;
  770.           definition = (char *) xrealloc (definition, length + 1);
  771.         }
  772.  
  773.       bcopy (p, &definition[idx], len);
  774.       idx += len;
  775.       /* Separate lines with a newline.  */
  776.       definition[idx++] = '\n';
  777.     }
  778.     }
  779.  
  780.   /* No `endef'!!  */
  781.   makefile_fatal (filename, lineno, "missing `endef', unterminated `define'");
  782.  
  783.   /* NOTREACHED */
  784.   return 0;
  785. }
  786.  
  787. /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
  788.    "ifneq", "else" and "endif".
  789.    LINE is the input line, with the command as its first word.
  790.  
  791.    FILENAME and LINENO are the filename and line number in the
  792.    current makefile.  They are used for error messages.
  793.  
  794.    Value is -1 if the line is invalid,
  795.    0 if following text should be interpreted,
  796.    1 if following text should be ignored.  */
  797.  
  798. static int
  799. conditional_line (line, filename, lineno)
  800.      char *line;
  801.      char *filename;
  802.      unsigned int lineno;
  803. {
  804.   int notdef;
  805.   char *cmdname;
  806.   register unsigned int i;
  807.  
  808.   if (*line == 'i')
  809.     {
  810.       /* It's an "if..." command.  */
  811.       notdef = line[2] == 'n';
  812.       if (notdef)
  813.     {
  814.       cmdname = line[3] == 'd' ? "ifndef" : "ifneq";
  815.       line += cmdname[3] == 'd' ? 7 : 6;
  816.     }
  817.       else
  818.     {
  819.       cmdname = line[2] == 'd' ? "ifdef" : "ifeq";
  820.       line += cmdname[2] == 'd' ? 6 : 5;
  821.     }
  822.     }
  823.   else
  824.     {
  825.       /* It's an "else" or "endif" command.  */
  826.       notdef = line[1] == 'n';
  827.       cmdname = notdef ? "endif" : "else";
  828.       line += notdef ? 5 : 4;
  829.     }
  830.  
  831.   line = next_token (line);
  832.  
  833.   if (*cmdname == 'e')
  834.     {
  835.       if (*line != '\0')
  836.     makefile_error (filename, lineno,
  837.             "Extraneous text after `%s' directive",
  838.             cmdname);
  839.       /* "Else" or "endif".  */
  840.       if (conditionals->if_cmds == 0)
  841.     makefile_fatal (filename, lineno, "extraneous `%s'", cmdname);
  842.       /* NOTDEF indicates an `endif' command.  */
  843.       if (notdef)
  844.     --conditionals->if_cmds;
  845.       else if (conditionals->seen_else[conditionals->if_cmds - 1])
  846.     makefile_fatal (filename, lineno, "only one `else' per conditional");
  847.       else
  848.     {
  849.       /* Toggle the state of ignorance.  */
  850.       conditionals->ignoring[conditionals->if_cmds - 1]
  851.         = !conditionals->ignoring[conditionals->if_cmds - 1];
  852.       /* Record that we have seen an `else' in this conditional.
  853.          A second `else' will be erroneous.  */
  854.       conditionals->seen_else[conditionals->if_cmds - 1] = 1;
  855.     }
  856.       for (i = 0; i < conditionals->if_cmds; ++i)
  857.     if (conditionals->ignoring[i])
  858.       return 1;
  859.       return 0;
  860.     }
  861.  
  862.   if (conditionals->allocated == 0)
  863.     {
  864.       conditionals->allocated = 5;
  865.       conditionals->ignoring = (char *) xmalloc (conditionals->allocated);
  866.       conditionals->seen_else = (char *) xmalloc (conditionals->allocated);
  867.     }
  868.  
  869.   ++conditionals->if_cmds;
  870.   if (conditionals->if_cmds > conditionals->allocated)
  871.     {
  872.       conditionals->allocated += 5;
  873.       conditionals->ignoring = (char *)
  874.     xrealloc (conditionals->ignoring, conditionals->allocated);
  875.       conditionals->seen_else = (char *)
  876.     xrealloc (conditionals->seen_else, conditionals->allocated);
  877.     }
  878.  
  879.   /* Record that we have seen an `if...' but no `else' so far.  */
  880.   conditionals->seen_else[conditionals->if_cmds - 1] = 0;
  881.  
  882.   /* Search through the stack to see if we're already ignoring.  */
  883.   for (i = 0; i < conditionals->if_cmds - 1; ++i)
  884.     if (conditionals->ignoring[i])
  885.       {
  886.     /* We are already ignoring, so just push a level
  887.        to match the next "else" or "endif", and keep ignoring.
  888.        We don't want to expand variables in the condition.  */
  889.     conditionals->ignoring[conditionals->if_cmds - 1] = 1;
  890.     return 1;
  891.       }
  892.  
  893.   if (cmdname[notdef ? 3 : 2] == 'd')
  894.     {
  895.       /* "Ifdef" or "ifndef".  */
  896.       struct variable *v;
  897.       register char *p = end_of_token (line);
  898.       i = p - line;
  899.       p = next_token (p);
  900.       if (*p != '\0')
  901.     return -1;
  902.       v = lookup_variable (line, i);
  903.       conditionals->ignoring[conditionals->if_cmds - 1]
  904.     = (v != 0 && *v->value != '\0') == notdef;
  905.     }
  906.   else
  907.     {
  908.       /* "Ifeq" or "ifneq".  */
  909.       char *s1, *s2;
  910.       unsigned int len;
  911.       char termin = *line == '(' ? ',' : *line;
  912.  
  913.       if (termin != ',' && termin != '"' && termin != '\'')
  914.     return -1;
  915.  
  916.       s1 = ++line;
  917.       /* Find the end of the first string.  */
  918.       if (termin == ',')
  919.     {
  920.       register int count = 0;
  921.       for (; *line != '\0'; ++line)
  922.         if (*line == '(')
  923.           ++count;
  924.         else if (*line == ')')
  925.           --count;
  926.         else if (*line == ',' && count <= 0)
  927.           break;
  928.     }
  929.       else
  930.     while (*line != '\0' && *line != termin)
  931.       ++line;
  932.  
  933.       if (*line == '\0')
  934.     return -1;
  935.  
  936.       *line++ = '\0';
  937.  
  938.       s2 = variable_expand (s1);
  939.       /* We must allocate a new copy of the expanded string because
  940.      variable_expand re-uses the same buffer.  */
  941.       len = strlen (s2);
  942.       s1 = (char *) alloca (len + 1);
  943.       bcopy (s2, s1, len + 1);
  944.  
  945.       if (termin != ',')
  946.     /* Find the start of the second string.  */
  947.     line = next_token (line);
  948.  
  949.       termin = termin == ',' ? ')' : *line;
  950.       if (termin != ')' && termin != '"' && termin != '\'')
  951.     return -1;
  952.  
  953.       /* Find the end of the second string.  */
  954.       if (termin == ')')
  955.     {
  956.       register int count = 0;
  957.       s2 = next_token (line);
  958.       for (line = s2; *line != '\0'; ++line)
  959.         {
  960.           if (*line == '(')
  961.         ++count;
  962.           else if (*line == ')')
  963.         if (count <= 0)
  964.           break;
  965.         else
  966.           --count;
  967.         }
  968.     }
  969.       else
  970.     {
  971.       ++line;
  972.       s2 = line;
  973.       while (*line != '\0' && *line != termin)
  974.         ++line;
  975.     }
  976.  
  977.       if (*line == '\0')
  978.     return -1;
  979.  
  980.       *line = '\0';
  981.       line = next_token (++line);
  982.       if (*line != '\0')
  983.     makefile_error (filename, lineno,
  984.             "Extraneous text after `%s' directive",
  985.             cmdname);
  986.  
  987.       s2 = variable_expand (s2);
  988.       conditionals->ignoring[conditionals->if_cmds - 1]
  989.     = streq (s1, s2) == notdef;
  990.     }
  991.  
  992.   /* Search through the stack to see if we're ignoring.  */
  993.   for (i = 0; i < conditionals->if_cmds; ++i)
  994.     if (conditionals->ignoring[i])
  995.       return 1;
  996.   return 0;
  997. }
  998.  
  999. /* Remove duplicate dependencies in CHAIN.  */
  1000.  
  1001. void
  1002. uniquize_deps (chain)
  1003.      struct dep *chain;
  1004. {
  1005.   register struct dep *d;
  1006.  
  1007.   /* Make sure that no dependencies are repeated.  This does not
  1008.      really matter for the purpose of updating targets, but it
  1009.      might make some names be listed twice for $^ and $?.  */
  1010.  
  1011.   for (d = chain; d != 0; d = d->next)
  1012.     {
  1013.       struct dep *last, *next;
  1014.  
  1015.       last = d;
  1016.       next = d->next;
  1017.       while (next != 0)
  1018.     if (streq (dep_name (d), dep_name (next)))
  1019.       {
  1020.         struct dep *n = next->next;
  1021.         last->next = n;
  1022.         if (next->name != 0 && next->name != d->name)
  1023.           free (next->name);
  1024.         if (next != d)
  1025.           free ((char *) next);
  1026.         next = n;
  1027.       }
  1028.     else
  1029.       {
  1030.         last = next;
  1031.         next = next->next;
  1032.       }
  1033.     }
  1034. }
  1035.  
  1036. /* Record a description line for files FILENAMES,
  1037.    with dependencies DEPS, commands to execute described
  1038.    by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
  1039.    TWO_COLON is nonzero if a double colon was used.
  1040.    If not nil, PATTERN is the `%' pattern to make this
  1041.    a static pattern rule, and PATTERN_PERCENT is a pointer
  1042.    to the `%' within it.
  1043.  
  1044.    The links of FILENAMES are freed, and so are any names in it
  1045.    that are not incorporated into other data structures.  */
  1046.  
  1047. static void
  1048. record_files (filenames, pattern, pattern_percent, deps, commands_started,
  1049.           commands, commands_idx, two_colon, filename, lineno, set_default)
  1050.      struct nameseq *filenames;
  1051.      char *pattern, *pattern_percent;
  1052.      struct dep *deps;
  1053.      unsigned int commands_started;
  1054.      char *commands;
  1055.      unsigned int commands_idx;
  1056.      int two_colon;
  1057.      char *filename;
  1058.      unsigned int lineno;
  1059.      int set_default;
  1060. {
  1061.   struct nameseq *nextf;
  1062.   int implicit = 0;
  1063.   unsigned int max_targets, target_idx;
  1064.   char **targets = 0, **target_percents = 0;
  1065.   struct commands *cmds;
  1066.  
  1067.   if (commands_idx > 0)
  1068.     {
  1069.       cmds = (struct commands *) xmalloc (sizeof (struct commands));
  1070.       cmds->filename = filename;
  1071.       cmds->lineno = commands_started;
  1072.       cmds->commands = savestring (commands, commands_idx);
  1073.       cmds->command_lines = 0;
  1074.     }
  1075.   else
  1076.     cmds = 0;
  1077.  
  1078.   for (; filenames != 0; filenames = nextf)
  1079.     {
  1080.       register char *name = filenames->name;
  1081.       register struct file *f;
  1082.       register struct dep *d;
  1083.       struct dep *this;
  1084.       char *implicit_percent;
  1085.  
  1086.       nextf = filenames->next;
  1087.       free ((char *) filenames);
  1088.  
  1089.       implicit_percent = find_percent (name);
  1090.       implicit |= implicit_percent != 0;
  1091.  
  1092.       if (implicit && pattern != 0)
  1093.     makefile_fatal (filename, lineno,
  1094.             "mixed implicit and static pattern rules");
  1095.  
  1096.       if (implicit && implicit_percent == 0)
  1097.     makefile_fatal (filename, lineno, "mixed implicit and normal rules");
  1098.  
  1099.       if (implicit)
  1100.     {
  1101.       if (targets == 0)
  1102.         {
  1103.           max_targets = 5;
  1104.           targets = (char **) xmalloc (5 * sizeof (char *));
  1105.           target_percents = (char **) xmalloc (5 * sizeof (char *));
  1106.           target_idx = 0;
  1107.         }
  1108.       else if (target_idx == max_targets - 1)
  1109.         {
  1110.           max_targets += 5;
  1111.           targets = (char **) xrealloc ((char *) targets,
  1112.                         max_targets * sizeof (char *));
  1113.           target_percents
  1114.         = (char **) xrealloc ((char *) target_percents,
  1115.                       max_targets * sizeof (char *));
  1116.         }
  1117.       targets[target_idx] = name;
  1118.       target_percents[target_idx] = implicit_percent;
  1119.       ++target_idx;
  1120.       continue;
  1121.     }
  1122.  
  1123.       /* If there are multiple filenames, copy the chain DEPS
  1124.      for all but the last one.  It is not safe for the same deps
  1125.      to go in more than one place in the data base.  */
  1126.       this = nextf != 0 ? copy_dep_chain (deps) : deps;
  1127.  
  1128.       if (pattern != 0)
  1129.     /* If this is an extended static rule:
  1130.        `targets: target%pattern: dep%pattern; cmds',
  1131.        translate each dependency pattern into a plain filename
  1132.        using the target pattern and this target's name.  */
  1133.     if (!pattern_matches (pattern, pattern_percent, name))
  1134.       {
  1135.         /* Give a warning if the rule is meaningless.  */
  1136.         makefile_error (filename, lineno,
  1137.                 "target `%s' doesn't match the target pattern",
  1138.                 name);
  1139.         this = 0;
  1140.       }
  1141.     else
  1142.       {
  1143.         /* We use patsubst_expand to do the work of translating
  1144.            the target pattern, the target's name and the dependencies'
  1145.            patterns into plain dependency names.  */
  1146.         char *buffer = variable_expand ("");
  1147.  
  1148.         for (d = this; d != 0; d = d->next)
  1149.           {
  1150.         char *o;
  1151.         char *percent = find_percent (d->name);
  1152.         if (percent == 0)
  1153.           continue;
  1154.         o = patsubst_expand (buffer, name, pattern, d->name,
  1155.                      pattern_percent, percent);
  1156.         free (d->name);
  1157.         d->name = savestring (buffer, o - buffer);
  1158.           }
  1159.       }
  1160.       
  1161.       if (!two_colon)
  1162.     {
  1163.       /* Single-colon.  Combine these dependencies
  1164.          with others in file's existing record, if any.  */
  1165.       f = enter_file (name);
  1166.  
  1167.       if (f->double_colon)
  1168.         makefile_fatal (filename, lineno,
  1169.                 "target file `%s' has both : and :: entries",
  1170.                 f->name);
  1171.  
  1172.       /* If CMDS == F->CMDS, this target was listed in this rule
  1173.          more than once.  Just give a warning since this is harmless.  */
  1174.       if (cmds != 0 && cmds == f->cmds)
  1175.         makefile_error
  1176.           (filename, lineno,
  1177.            "target `%s' given more than once in the same rule.",
  1178.            f->name);
  1179.  
  1180.       /* Check for two single-colon entries both with commands.
  1181.          Check is_target so that we don't lose on files such as .c.o
  1182.          whose commands were preinitialized.  */
  1183.       else if (cmds != 0 && f->cmds != 0 && f->is_target)
  1184.         {
  1185.           makefile_error (cmds->filename, cmds->lineno,
  1186.                   "warning: overriding commands for target `%s'",
  1187.                   f->name);
  1188.           makefile_error (f->cmds->filename, f->cmds->lineno,
  1189.                   "warning: ignoring old commands for target `%s'",
  1190.                   f->name);
  1191.         }
  1192.  
  1193.       f->is_target = 1;
  1194.  
  1195.       /* Defining .DEFAULT with no deps or cmds clears it.  */
  1196.       if (f == default_file && this == 0 && cmds == 0)
  1197.         f->cmds = 0;
  1198.       if (cmds != 0)
  1199.         f->cmds = cmds;
  1200.       /* Defining .SUFFIXES with no dependencies
  1201.          clears out the list of suffixes.  */
  1202.       if (f == suffix_file && this == 0)
  1203.         {
  1204.           d = f->deps;
  1205.           while (d != 0)
  1206.         {
  1207.           struct dep *nextd = d->next;
  1208.            free (d->name);
  1209.            free (d);
  1210.           d = nextd;
  1211.         }
  1212.           f->deps = 0;
  1213.         }
  1214.       else if (f->deps != 0)
  1215.         {
  1216.           /* Add the file's old deps and the new ones in THIS together.  */
  1217.  
  1218.           struct dep *firstdeps, *moredeps;
  1219.           if (cmds != 0)
  1220.         {
  1221.           /* This is the rule with commands, so put its deps first.
  1222.              The rationale behind this is that $< expands to the
  1223.              first dep in the chain, and commands use $< expecting
  1224.              to get the dep that rule specifies.  */
  1225.           firstdeps = this;
  1226.           moredeps = f->deps;
  1227.         }
  1228.           else
  1229.         {
  1230.           /* Append the new deps to the old ones.  */
  1231.           firstdeps = f->deps;
  1232.           moredeps = this;
  1233.         }
  1234.  
  1235.           if (firstdeps == 0)
  1236.         firstdeps = moredeps;
  1237.           else
  1238.         {
  1239.           d = firstdeps;
  1240.           while (d->next != 0)
  1241.             d = d->next;
  1242.           d->next = moredeps;
  1243.         }
  1244.  
  1245.           f->deps = firstdeps;
  1246.         }
  1247.       else
  1248.         f->deps = this;
  1249.  
  1250.       /* If this is a static pattern rule, set the file's stem to
  1251.          the part of its name that matched the `%' in the pattern,
  1252.          so you can use $* in the commands.  */
  1253.       if (pattern != 0)
  1254.         {
  1255.           static char *percent = "%";
  1256.           char *buffer = variable_expand ("");
  1257.           char *o = patsubst_expand (buffer, name, pattern, percent,
  1258.                      pattern_percent, percent);
  1259.           f->stem = savestring (buffer, o - buffer);
  1260.         }
  1261.     }
  1262.       else
  1263.     {
  1264.       /* Double-colon.  Make a new record
  1265.          even if the file already has one.  */
  1266.       f = lookup_file (name);
  1267.       /* Check for both : and :: rules.  Check is_target so
  1268.          we don't lose on default suffix rules or makefiles.  */
  1269.       if (f != 0 && f->is_target && !f->double_colon)
  1270.         makefile_fatal (filename, lineno,
  1271.                 "target file `%s' has both : and :: entries",
  1272.                 f->name);
  1273.       f = enter_file (name);
  1274.       /* If there was an existing entry and it was a
  1275.          double-colon entry, enter_file will have returned a
  1276.          new one, making it the prev pointer of the old one.  */
  1277.       f->is_target = 1;
  1278.       f->double_colon = 1;
  1279.       f->deps = this;
  1280.       f->cmds = cmds;
  1281.     }
  1282.  
  1283.       /* Free name if not needed further.  */
  1284.       if (f != 0 && name != f->name
  1285.       && (name < f->name || name > f->name + strlen (f->name)))
  1286.     {
  1287.       free (name);
  1288.       name = f->name;
  1289.     }
  1290.  
  1291.       /* See if this is first target seen whose name does
  1292.      not start with a `.', unless it contains a slash.  */
  1293.       if (default_goal_file == 0 && set_default
  1294.       && (*name != '.' || index (name, '/') != 0))
  1295.     {
  1296.       int reject = 0;
  1297.  
  1298.       /* If this file is a suffix, don't
  1299.          let it be the default goal file.  */
  1300.  
  1301.       for (d = suffix_file->deps; d != 0; d = d->next)
  1302.         {
  1303.           register struct dep *d2;
  1304.           if (*dep_name (d) != '.' && streq (name, dep_name (d)))
  1305.         {
  1306.           reject = 1;
  1307.           break;
  1308.         }
  1309.           for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
  1310.         {
  1311.           register unsigned int len = strlen (dep_name (d2));
  1312.           if (strncmp (name, dep_name (d2), len))
  1313.             continue;
  1314.           if (streq (name + len, dep_name (d)))
  1315.             {
  1316.               reject = 1;
  1317.               break;
  1318.             }
  1319.         }
  1320.           if (reject)
  1321.         break;
  1322.         }
  1323.  
  1324.       if (!reject)
  1325.         default_goal_file = f;
  1326.     }
  1327.     }
  1328.  
  1329.   if (implicit)
  1330.     {
  1331.       targets[target_idx] = 0;
  1332.       target_percents[target_idx] = 0;
  1333.       create_pattern_rule (targets, target_percents, two_colon, deps, cmds, 1);
  1334.       free ((char *) target_percents);
  1335.     }
  1336. }
  1337.  
  1338. /* Search STRING for an unquoted ; that is not after an unquoted #.  */
  1339.  
  1340. static char *
  1341. find_semicolon (string)
  1342.      char *string;
  1343. {
  1344.   char *found, *p;
  1345.  
  1346.   found = index (string, ';');
  1347.   while (found != 0 && found[-1] == '\\')
  1348.     {
  1349.       register char *q = &found[-1];
  1350.       register int backslash = 0;
  1351.       while (*q-- == '\\')
  1352.     backslash = !backslash;
  1353.       if (backslash)
  1354.     found = index (found + 1, ';');
  1355.       else
  1356.     break;
  1357.     }
  1358.   if (found == 0)
  1359.     return 0;
  1360.  
  1361.   /* Look for a comment character (#) before the ; we found.  */
  1362.   p = lindex (string, found, '#');
  1363.   while (p != 0 && p[-1] == '\\')
  1364.     {
  1365.       register char *q = &p[-1];
  1366.       register int backslash = 0;
  1367.       while (*q-- == '\\')
  1368.     backslash = !backslash;
  1369.       if (backslash)
  1370.     p = lindex (p + 1, found, '#');
  1371.       else
  1372.     break;
  1373.     }
  1374.   if (p == 0)
  1375.     return found;
  1376.   return 0;
  1377. }
  1378.  
  1379. /* Search PATTERN for an unquoted %.  Backslashes quote % and backslash.
  1380.    Quoting backslashes are removed from PATTERN by compacting it into
  1381.    itself.  Returns a pointer to the first unquoted % if there is one,
  1382.    or nil if there are none.  */
  1383.  
  1384. char *
  1385. find_percent (pattern)
  1386.      char *pattern;
  1387. {
  1388.   unsigned int pattern_len = strlen (pattern);
  1389.   register char *p = pattern;
  1390.  
  1391.   while ((p = index (p, '%')) != 0)
  1392.     if (p > pattern && p[-1] == '\\')
  1393.       {
  1394.     /* Search for more backslashes.  */
  1395.     register int i = -2;
  1396.     while (&p[i] >= pattern && p[i] == '\\')
  1397.       --i;
  1398.     ++i;
  1399.     /* The number of backslashes is now -I.
  1400.        Copy P over itself to swallow half of them.  */
  1401.     bcopy (&p[i / 2], &p[i], (pattern_len - (p - pattern)) - (i / 2) + 1);
  1402.     p += i / 2;
  1403.     if (i % 2 == 0)
  1404.       /* All the backslashes quoted each other; the % was unquoted.  */
  1405.       return p;
  1406.  
  1407.     /* The % was quoted by a backslash.  Look for another.  */
  1408.       }
  1409.     else
  1410.       /* No backslash in sight.  */
  1411.       return p;
  1412.  
  1413.   /* Never hit a %.  */
  1414.   return 0;
  1415. }
  1416.  
  1417. /* Parse a string into a sequence of filenames represented as a
  1418.    chain of struct nameseq's in reverse order and return that chain.
  1419.  
  1420.    The string is passed as STRINGP, the address of a string pointer.
  1421.    The string pointer is updated to point at the first character
  1422.    not parsed, which either is a null char or equals STOPCHAR.
  1423.  
  1424.    SIZE is how big to construct chain elements.
  1425.    This is useful if we want them actually to be other structures
  1426.    that have room for additional info.
  1427.  
  1428.    If STRIP is nonzero, strip `./'s off the beginning.  */
  1429.  
  1430. struct nameseq *
  1431. parse_file_seq (stringp, stopchar, size, strip)
  1432.      char **stringp;
  1433.      char stopchar;
  1434.      unsigned int size;
  1435.      int strip;
  1436. {
  1437.   register struct nameseq *new = 0;
  1438.   register struct nameseq *new1;
  1439.   register char *p = *stringp;
  1440.   char *q;
  1441.   char *name;
  1442.   register int c;
  1443.  
  1444.   while (1)
  1445.     {
  1446.       /* Skip whitespace; see if any more names are left.  */
  1447.       p = next_token (p);
  1448.       if (*p == '\0')
  1449.     break;
  1450.       if (*p == stopchar)
  1451.     break;
  1452.       /* Yes, find end of next name.  */
  1453.       q = p;
  1454.       while (1)
  1455.     {
  1456.       c = *p++;
  1457.       if (c == '\0')
  1458.         break;
  1459.       else if (c == '\\' &&
  1460.                (*p == '\\' || isblank (*p) || *p == stopchar))
  1461.         ++p;
  1462.       else if (isblank (c) || c == stopchar)
  1463.         break;
  1464.     }
  1465.       p--;
  1466.  
  1467.       if (strip)
  1468.     /* Skip leading `./'s.  */
  1469.     while (p - q > 2 && q[0] == '.' && q[1] == '/')
  1470.       {
  1471.         q += 2;        /* Skip "./".  */
  1472.         while (q < p && *q == '/')
  1473.           /* Skip following slashes: ".//foo" is "foo", not "/foo".  */
  1474.           ++q;
  1475.       }
  1476.  
  1477.       /* Extract the filename just found, and skip it.  */
  1478.  
  1479.       if (q == p)
  1480.     /* ".///" was stripped to "".  */
  1481.     name = savestring ("./", 2);
  1482.       else
  1483.     name = savestring (q, p - q);
  1484.  
  1485.       /* Add it to the front of the chain.  */
  1486.       new1 = (struct nameseq *) xmalloc (size);
  1487.       new1->name = name;
  1488.       new1->next = new;
  1489.       new = new1;
  1490.     }
  1491.  
  1492.   *stringp = p;
  1493.   return new;
  1494. }
  1495.  
  1496. /* Read a line of text from STREAM into LINEBUFFER.
  1497.    Combine continuation lines into one line.
  1498.    Return the number of actual lines read (> 1 if hacked continuation lines).
  1499.  */
  1500.  
  1501. static unsigned int
  1502. readline (linebuffer, stream, filename, lineno)
  1503.      struct linebuffer *linebuffer;
  1504.      FILE *stream;
  1505.      char *filename;
  1506.      unsigned int lineno;
  1507. {
  1508.   char *buffer = linebuffer->buffer;
  1509.   register char *p = linebuffer->buffer;
  1510.   register char *end = p + linebuffer->size;
  1511.   register int len, lastlen = 0;
  1512.   register char *p2;
  1513.   register unsigned int nlines = 0;
  1514.   register int backslash;
  1515.  
  1516.   *p = '\0';
  1517.  
  1518.   while (1)
  1519.     {
  1520.       if (fgets (p, end - p, stream) == 0)
  1521.     if (feof (stream))
  1522.       break;
  1523.     else
  1524.       pfatal_with_name (filename);
  1525.  
  1526.       len = strlen (p);
  1527.       if (len == 0)
  1528.     /* This only happens when the first thing on the line is a '\0'.  */
  1529.     makefile_fatal (filename, lineno, "NUL not allowed in makefile");
  1530.  
  1531.       p += len;
  1532.       if (p[-1] != '\n')
  1533.     {
  1534.       /* Probably ran out of buffer space.  */
  1535.       register unsigned int p_off = p - buffer;
  1536.       linebuffer->size *= 2;
  1537.       buffer = (char *) xrealloc (buffer, linebuffer->size);
  1538.       p = buffer + p_off;
  1539.       end = buffer + linebuffer->size;
  1540.       linebuffer->buffer = buffer;
  1541.       *p = '\0';
  1542.       lastlen = len;
  1543.       continue;
  1544.     }
  1545.  
  1546.       ++nlines;
  1547.  
  1548.       if (len == 1 && p > buffer)
  1549.     /* P is pointing at a newline and it's the beginning of
  1550.        the buffer returned by the last fgets call.  However,
  1551.        it is not necessarily the beginning of a line if P is
  1552.        pointing past the beginning of the holding buffer.
  1553.        If the buffer was just enlarged (right before the newline),
  1554.        we must account for that, so we pretend that the two lines
  1555.        were one line.  */
  1556.     len += lastlen;
  1557.       lastlen = len;
  1558.       backslash = 0;
  1559.       for (p2 = p - 2; --len > 0; --p2)
  1560.     {
  1561.       if (*p2 == '\\')
  1562.         backslash = !backslash;
  1563.       else
  1564.         break;
  1565.     }
  1566.       
  1567.       if (!backslash)
  1568.     {
  1569.       p[-1] = '\0';
  1570.       break;
  1571.     }
  1572.  
  1573.       if (end - p <= 1)
  1574.     {
  1575.       /* Enlarge the buffer.  */
  1576.       register unsigned int p_off = p - buffer;
  1577.       linebuffer->size *= 2;
  1578.       buffer = (char *) xrealloc (buffer, linebuffer->size);
  1579.       p = buffer + p_off;
  1580.       end = buffer + linebuffer->size;
  1581.       linebuffer->buffer = buffer;
  1582.     }
  1583.     }
  1584.  
  1585.   return nlines;
  1586. }
  1587.  
  1588. /* Construct the list of include directories
  1589.    from the arguments and the default list.  */
  1590.  
  1591. void
  1592. construct_include_path (arg_dirs)
  1593.      char **arg_dirs;
  1594. {
  1595.   register unsigned int i;
  1596.   struct stat stbuf;
  1597.  
  1598.   /* Table to hold the dirs.  */
  1599.  
  1600.   register unsigned int defsize = (sizeof (default_include_directories)
  1601.                    / sizeof (default_include_directories[0]));
  1602.   register unsigned int max = 5;
  1603.   register char **dirs = (char **) xmalloc ((5 + defsize) * sizeof (char *));
  1604.   register unsigned int idx = 0;
  1605.  
  1606.   /* First consider any dirs specified with -I switches.
  1607.      Ignore dirs that don't exist.  */
  1608.  
  1609.   if (arg_dirs != 0)
  1610.     while (*arg_dirs != 0)
  1611.       {
  1612.     char *dir = *arg_dirs++;
  1613.     if (stat (dir, &stbuf) == 0 && S_ISDIR (stbuf.st_mode))
  1614.       {
  1615.         if (idx == max - 1)
  1616.           {
  1617.         max += 5;
  1618.         dirs = (char **)
  1619.           xrealloc ((char *) dirs, (max + defsize) * sizeof (char *));
  1620.           }
  1621.         dirs[idx++] = dir;
  1622.       }
  1623.       }
  1624.  
  1625.   /* Now add at the end the standard default dirs.  */
  1626.  
  1627.   for (i = 0; default_include_directories[i] != 0; ++i)
  1628.     if (stat (default_include_directories[i], &stbuf) == 0
  1629.     && S_ISDIR (stbuf.st_mode))
  1630.       dirs[idx++] = default_include_directories[i];
  1631.  
  1632.   dirs[idx] = 0;
  1633.  
  1634.   /* Now compute the maximum length of any name in it.  */
  1635.  
  1636.   max_incl_len = 0;
  1637.   for (i = 0; i < idx; ++i)
  1638.     {
  1639.       unsigned int len = strlen (dirs[i]);
  1640.       /* If dir name is written with a trailing slash, discard it.  */
  1641.       if (dirs[i][len - 1] == '/')
  1642.     /* We can't just clobber a null in because it may have come from
  1643.        a literal string and literal strings may not be writable.  */
  1644.     dirs[i] = savestring (dirs[i], len - 1);
  1645.       if (len > max_incl_len)
  1646.     max_incl_len = len;
  1647.     }
  1648.  
  1649.   include_directories = dirs;
  1650. }
  1651.  
  1652. /* Given a chain of struct nameseq's describing a sequence of filenames,
  1653.    in reverse of the intended order, return a new chain describing the
  1654.    result of globbing the filenames.  The new chain is in forward order.
  1655.    The links of the old chain are freed or used in the new chain.
  1656.    Likewise for the names in the old chain.
  1657.  
  1658.    SIZE is how big to construct chain elements.
  1659.    This is useful if we want them actually to be other structures
  1660.    that have room for additional info.  */
  1661.  
  1662. struct nameseq *
  1663. multi_glob (chain, size)
  1664.      struct nameseq *chain;
  1665.      unsigned int size;
  1666. {
  1667.   register struct nameseq *new = 0;
  1668.   register struct nameseq *old;
  1669.   struct nameseq *nexto;
  1670.  
  1671.   for (old = chain; old != 0; old = nexto)
  1672.     {
  1673.       glob_t gl;
  1674.  
  1675.       nexto = old->next;
  1676.  
  1677.       if (old->name[0] == '~')
  1678.     {
  1679.       if (old->name[1] == '/' || old->name[1] == '\0')
  1680.         {
  1681.           extern char *getenv ();
  1682.           char *home_dir = allocated_variable_expand ("$(HOME)");
  1683.           int is_variable = home_dir[0] != '\0';
  1684.           if (!is_variable)
  1685.         {
  1686.           free (home_dir);
  1687.           home_dir = getenv ("HOME");
  1688.         }
  1689.           if (home_dir == 0 || home_dir[0] == '\0')
  1690.         {
  1691.           extern char *getlogin ();
  1692.           char *name = getlogin ();
  1693.           home_dir = 0;
  1694.           if (name != 0)
  1695.             {
  1696.               struct passwd *p = getpwnam (name);
  1697.               if (p != 0)
  1698.             home_dir = p->pw_dir;
  1699.             }
  1700.         }
  1701.           if (home_dir != 0)
  1702.         {
  1703.           char *new = concat (home_dir, "", old->name + 1);
  1704.           if (is_variable)
  1705.             free (home_dir);
  1706.           free (old->name);
  1707.           old->name = new;
  1708.         }
  1709.         }
  1710.       else
  1711.         {
  1712.           struct passwd *pwent;
  1713.           char *userend = index (old->name + 1, '/');
  1714.           if (userend != 0)
  1715.         *userend = '\0';
  1716.           pwent = getpwnam (old->name + 1);
  1717.           if (pwent != 0)
  1718.         {
  1719.           if (userend == 0)
  1720.             {
  1721.               free (old->name);
  1722.               old->name = savestring (pwent->pw_dir,
  1723.                           strlen (pwent->pw_dir));
  1724.             }
  1725.           else
  1726.             {
  1727.               char *new = concat (pwent->pw_dir, "/", userend + 1);
  1728.               free (old->name);
  1729.               old->name = new;
  1730.             }
  1731.         }
  1732.           else if (userend != 0)
  1733.         *userend = '/';
  1734.         }
  1735.     }
  1736.  
  1737.       switch (glob (old->name, GLOB_NOCHECK, NULL, &gl))
  1738.     {
  1739.     case 0:            /* Success.  */
  1740.       {
  1741.         register int i = gl.gl_pathc;
  1742.         while (i-- > 0)
  1743.           {
  1744.         struct nameseq *elt = (struct nameseq *) xmalloc (size);
  1745.         elt->name = savestring (gl.gl_pathv[i],
  1746.                     strlen (gl.gl_pathv[i]));
  1747.         elt->next = new;
  1748.         new = elt;
  1749.           }
  1750.         globfree (&gl);
  1751.         free (old->name);
  1752.         free (old);
  1753.         break;
  1754.       }
  1755.  
  1756.     case GLOB_NOSPACE:
  1757.       fatal ("virtual memory exhausted");
  1758.       break;
  1759.  
  1760.     default:
  1761.       old->next = new;
  1762.       new = old;
  1763.       break;
  1764.     }
  1765.     }
  1766.  
  1767.   return new;
  1768. }
  1769.